import {Routers, simpleRouter, mainRouter, appRouter} from '@/router/router';

const app = {
    state: {
        // 需要缓存的页面
        cachePage: [],
        // 菜单列表
        menuList: [],
    },
    mutations: {
        // 更新菜单，这里可以做权限判断，显示需要展示的菜单
        updateMenulist (state) {
            state.menuList = appRouter.filter(menu => {
                if(menu.meta && menu.meta.hidden) return false;
                return true;
            });
        },
        // 获取需要缓存的路由，只针对登录后的页面
        initCachePage(state) {
            let caches = [];
            let rs = [...simpleRouter, mainRouter, ...appRouter];
            
            function getCacheRouteName(routes) {
                for(let item in routes) {
                    if(routes[item].meta && routes[item].meta.cache) {
                        caches.push(routes[item].name);
                    }
                    if(routes[item].children && routes[item].children.length > 0) {
                        getCacheRouteName(routes[item].children);
                    }
                }
            }
            getCacheRouteName(rs);
            
            state.cachePage = caches;
        },
    }
};

export default app;
